package cn.bigcontainer.alt.workflow.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.bigcontainer.alt.common.exception.BigResultCode;
import cn.bigcontainer.alt.common.util.BigConstant;
import cn.bigcontainer.alt.meta.dto.MAppDto;
import cn.bigcontainer.alt.meta.dto.MCrud;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import cn.bigcontainer.alt.meta.po.MApp;
import cn.bigcontainer.alt.meta.po.MViewProperty;
import cn.bigcontainer.alt.workflow.dto.MWorkflow;
import cn.bigcontainer.alt.workflow.service.MWorkflowService;

@Controller
@RequestMapping("/meta/" + MAppDto.TYPE_WORKFLOW)
@SessionAttributes(BigConstant.SESSION_USER)
public class MWorkflowController {

	@Resource
	private MWorkflowService workflowService;

	@RequestMapping("")
	public String loadWorkflow(MCrud app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		req.putAll(map);
		MWorkflow workflow = workflowService.loadWorkflow(app.getId(), req);
		workflow.setDomprefix(app.getDomprefix());
		map.put("app", workflow);
		return "meta/" + MAppDto.TYPE_WORKFLOW;
	}

	/**
	 *
	 * @param app
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping("/loadList")
	public String loadListData(MCrud app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		MViewDto list = workflowService.loadListData(app.getId(), req);
		map.put("list", list);
		MCrud crud = (MCrud) list.getApp();
		crud.setDomprefix(app.getDomprefix());
		crud.setSelector(app.getSelector());
		return list.getApp().getListTemplate();
	}

	/**
	 * 加载创建页面
	 * @param app
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping("/loadCreateForm")
	public String loadCreateForm(MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		req.putAll(map);
		MWorkflow workflow = workflowService.loadCreateForm (app.getId(), null, req);
		workflow.setDomprefix(app.getDomprefix());
		map.put("app", workflow);
		return "meta/workflow-process";
	}

    @PostMapping("/create")
    @ResponseBody
    public BigResultCode create(MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		req.putAll(map);
        MViewDto form = workflowService.submitCreateForm(app.getId(), req);
        form.getApp().setDomprefix(app.getDomprefix());
        return BigResultCode.getSuccessCode();
    }

	/**
	 * 加载修改表单
	 * @param app
	 * @param selectedIds
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping("/loadUpdateForm")
	public String loadUpdateForm(MAppDto app, String selectedIds, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		req.putAll(map);
		MWorkflow workflow = workflowService.loadUpdateForm (app.getId(), selectedIds, req);
		workflow.setDomprefix(app.getDomprefix());
		map.put("app", workflow);
		return "meta/workflow-process";
	}

	/**
	 * 提交修改表单
	 * @param app
	 * @param req
	 * @param map
	 * @return
	 */
    @RequestMapping("/update")
	@ResponseBody
    public BigResultCode update (MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
    	req.putAll(map);
        MViewDto vw = workflowService.submitUpdateForm(app.getId(), req);
        return BigResultCode.getSuccessCode().setContent(vw.getCallBackData());
    }

	/**
	 *
	 * @param app
	 * @param selectedIds
	 * @param req
	 * @param map
	 * @return
	 */
    @RequestMapping("/delete")
	@ResponseBody
    public BigResultCode deleteInstances (MAppDto app, String selectedIds, @RequestParam Map<String, Object> req, Map<String, Object> map) {
        workflowService.deleteObjects(app.getId(), selectedIds, req);
        return BigResultCode.getSuccessCode();
    }

	/**
	 * 获取视图属性propertyId关联的crud的name值
	 * @param app
	 * @param propertyId
	 * @param propertyValue
	 * @param req
	 * @param map
	 * @return
	 */
	@RequestMapping("/loadSelectorNameValues")
    @ResponseBody
	public BigResultCode loadSelectorNameValues(MApp app, String propertyId, String propertyValue, @RequestParam Map<String, Object> req, Map<String, Object> map) {
		MViewProperty prop = workflowService.loadSelectorNameValues(app.getId(), propertyId, propertyValue, req);
		return BigResultCode.getSuccessCode().setContent(prop.getData().get(MViewProperty.PARAM_NAME_VALUE));
	}
}
